// pages/user/find/timeInfo/timeInfo.js
const app = getApp();
//解析html
const WxParse = require('../../../../wxParse/wxParse.js');
// 引入SDK核心类
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js');
var key = app.globalData.qqmapsdk;
var qqmapsdk = new QQMapWX({
  key: key
});
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // identityStatus: 1, //0为管理员身份，1为部门管理员
    tabBarIndex: 2, //tabBar对应的索引
    newsList: [], //资讯列表
    page: 1, //页码
    pageSize: 9, //每页显示条数
    input:'',//搜索关键
    searchFlag: true,
  },
  //获取输入关键字
  input:function(e){
    var that = this;
    that.setData({
      input: e.detail.value
    });
    this.getSearch();
  },
  //跳转 搜索关键字
  toSearchKeyWord: function() {
    //点击输入框
    this.setData({
      searchFlag: false
    })
    
  },
  //跳转 晒一晒
  baskToBask: function() {
    wx.navigateTo({
      url: '/pages/user/find/findBask/findBask',
    })
  },

  //跳转新闻/小知识列表
  toNewsList: function(e) {
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/news/newsList/newsList?type=' + type,
    })
  },
  //跳转详情
  toNewsDetails: function(e) {
    var that = this;
    var {
      newsList
    } = that.data
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    console.log(e.currentTarget.dataset)
    var lid = newsList[index].id
    // var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/news/newsDetails/newsDetails?lid=${lid}&type=${type}`,
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getNews();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },
  //搜索
  getSearch: function(e) {
    var that = this;
    // var newslist = that.data.newsList;
    var page = that.data.page;
    var param = {
      type: 5,
      pageNum: 1,
      pageSize: that.data.pageSize,
      keyWord:that.data.input
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          newsList:res.data,
        });
      }
    })
  },
  //新闻
  getNews: function() {
    var that = this;
    var newslist = that.data.newsList;
    var page = that.data.page;
    var param = {
      type: 5,
      pageNum: page,
      pageSize: that.data.pageSize,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        var data = res.data;
        if(res.data !=''){
          for(var i = 0; i<res.data.length;i++){
            if(data[i].typeName == '新闻资讯'){
              data[i]['typeTitle'] = 1;
            } else if(data[i].typeName == '分类小知识'){
              data[i]['typeTitle'] = 4;
            } else if(data[i].typeName == '晒一晒'){
              data[i]['typeTitle'] = 3;
            }
          }
          page++;
          that.setData({
            newsList: newslist.concat(res.data),
            page: page,
          });
        }

      } else {
        wx.showToast({
          icon: 'none',
          title: '暂时没有数据哦！',
        })


      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshData();
    wx.stopPullDownRefresh();
  },
  //下拉刷新
  refreshData: function() {
    var that = this;
    var param = {
      type: 5,
      pageNum: 1,
      pageSize: that.data.pageSize,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          newsList: res.data,
          page: 2,
        });
      } else {
        wx.showToast({
          icon: 'none',
          title: '暂时没有数据哦！',
        })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getNews();
  },
 


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})